const express = require('express');
const bodyparser = require('body-parser'); //body中间件
const adminRouter = require('./router/admin.js'); //管理菜单等路由
const synRouter = require('./router/syn.js'); //管理菜单等路由
const mallRouter = require('./router/mall.js'); //管理广西路由
const uploadRouter = require('./router/uploadOss.js'); //管理菜单等路由
const wnetRouter = require('./router/wnet.js'); //管理全网路由

const cors = require('cors'); //解决跨域的中间件
const server = express();
//注意要放到注册路由之前
// const session = require('express-session')
// server.use(
//     session({
//         secret: 'qjuuu213213',
//         resave: false,
//         saveUninitialized: true,
// 		cookie:{ // cookie的信息具体操作和`cookie-parser`一样
// 			secure: true,
// 			maxAge:300000// 
// 		  },
//     })
// )
const {errLog}= require("./utils/err");
const utils = require("./utils/index.js");
server.listen(3001);
server.keepAliveTimeout=30000;

server.use(cors({origin: "*",}));
server.use(express.static('./public')); //用户的静态资源
server.use(bodyparser.json());
var swaggerInstall=require('./router/swagger/swagger') //这里的路径是你刚刚创建js的路径
swaggerInstall(server);
// server.use(bodyparser.urlencoded({//body中间件
// 	extended:false
// }));
server.use(async function (req, res, next)  {
	if(req.headers.token){
		let user=await utils.getUserInfo(req,res);
		if(user.status===0) return res.send(utils.returnData({code: 203, msg: "你账号已被禁用，请联系管理员！！",req}));
	}
	next();
})
process.on('unhandledRejection', (err, test) => {
	errLog({err,code:500,msg:"后端系统错误！",funName:"fatal"});
	}).on('uncaughtException', err => {
	errLog({err,code:500,msg:"后端系统错误！！",funName:"fatal"});
	});

server.use('/admin', adminRouter); //挂载用户信息路由
server.use('/syn', synRouter); //挂载用户信息路由
server.use('/mall', mallRouter); //挂载用户信息路由
server.use('/upload', uploadRouter); //挂载用户信息路由
server.use('/wnet', wnetRouter); //挂载用户信息路由
console.log('后端接口启动成功');
